<template>
	<view class="signup">
		<!-- 添加选项卡 -->
		<view class="tabs">
			<view class="tab-item" :class="{ active: currentTab === 0 }" @click="switchTab(0)">
				待缴费
			</view>
			<view class="tab-item" :class="{ active: currentTab === 1 }" @click="switchTab(1)">
				已缴费
			</view>
		</view>
		<view class="item-container" @tap="goRouter(item.contractId)" v-for="(item, index) of displayedPaymentList" :key="index">
			<!-- 图片 -->
			<image class="item-image" :src="item.images[0]" mode="aspectFill"></image>

			<div class="left-item">
				<!-- 标题 -->
				<view class="item-title">{{ item.year }}年{{ item.month }}月{{ item.name }}</view>

				<!-- 描述 -->
				<view class="item-description">房屋 {{ item.housesId }}</view>

				<!-- 价格 -->
				<view class="item-buttom">
					<view class="item-price">
						<span v-if="item.piType === 2">¥{{ item.price }}</span>
						<span v-else>¥{{ item.price * item.dosage }} </span>
					</view>

					<!-- 添加按钮 -->
					<view v-if="currentTab === 0" class="action-button" @click.stop="pay(item.manageId)">缴费</view>
					<view v-if="currentTab === 1" class="action-button" @click.stop="refund(item)">申请退款</view>
				</view>
			</div>

			<!-- 分割线 -->
			<view class="item-divider"></view>
		</view>

		<view class="loading-text" ref="test">
			{{ finish ? '没有更多数据~' : '正在加载...' }}
		</view>

	</view>
</template>


<script setup lang="ts">
	import { ref, computed } from 'vue'
	import { createAliPayPage, getPaidList } from '@/services/payment'
	import { onLoad } from '@dcloudio/uni-app';
	import { PaymentDetails } from '/@/types/global';
	onLoad(async () => {
		const res = await getPaidList();
		console.log(res.data.list)
		paymentList.value = res.data.list;
	})
	const paymentList = ref<PaymentDetails[]>([
	]);
	// 定义当前选中的标签
	const currentTab = ref(0);

	// 切换标签的函数
	function switchTab(tabIndex : number) {
		currentTab.value = tabIndex;
	}


	// 计算属性来根据当前选中的标签返回不同的列表
	const displayedPaymentList = computed(() => {
		return currentTab.value === 0 ? paymentList.value : paymentList.value; // 这里假设待缴费和已缴费的数据相同，实际应用中应该不同
	});

	// 缴费和申请退款的函数
	const pay = async (manageId:number) => {
		// const res = await createAliPayPage("1");
		uni.navigateTo({
			url: `/pages/payment/alipay?manageId=${manageId}`
		})

	}

	function refund(item : any) {
		console.log('申请退款', item);
	}
	const goRouter = (contractId: number) => {
		// console.log(contractId)
		uni.navigateTo({
			url: `/pages/contract/contractDetail?contractId=${contractId}`
		})

	}
</script>

<style scoped lang="scss">
	.item-container {
		width: 345px;
		height: 136px;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.item-image {
		width: 130px;
		height: 90px;
		margin-right: 10px 0px;
		border-radius: 5px;
		margin-right: 10px;
		margin-left: 10px;
	}

	.item-address {
		display: flex;
	}

	.item-title {

		color: #10100c;
		font-size: 16px;
		line-height: 36px;

	}

	.item-description {
		font-size: 12px;
		color: #666;
		margin-bottom: 10px;
	}

	.item-address {
		font-size: 12px;
		color: #666;
		margin-bottom: 5px;
	}

	.item-feature-box {
		display: flex;
	}

	.item-feature {
		font-size: 11px;
		color: rgba(76, 95, 163, 0.698039215686274);
		line-height: 15px;
		width: 50px;
		text-align: center;
		margin: 0 5px;
		background-color: #edeff6;
	}

	.item-buttom {
		display: flex;
	}

	.item-price {
		font-size: 16px;
		color: #34b5bb;
		margin-bottom: 5px;
		padding-right: 20px;
	}

	.item-unit {
		font-size: 12px;
		color: #666;
	}

	.item-divider {
		border-top: 1px solid #eee;
		margin-top: 5px;
	}

	// 加载提示文字
	.loading-text {
		text-align: center;
		font-size: 28rpx;
		color: #666;
		padding: 20rpx 0;
	}

	.tabs {
		display: flex;
		justify-content: space-around;
		margin-bottom: 10px;
		font-size: 13px;

		.tab-item {
			padding: 10px;
			cursor: pointer;

			&.active {
				border-bottom: 1px solid #34b5bb;
				color: #34b5bb;
				font-weight: bold;
			}
		}
	}

	.action-button {
		margin-left: auto;
		padding: 5px 10px;
		background-color: #007bff;
		color: white;
		cursor: pointer;
		border-radius: 5px;
		font-size: 14px;
	}
</style>